package com.cxy.datawarehouse.threadLogin;



import com.cxy.datawarehouse.entity.User;
import com.cxy.datawarehouse.exception.BizException;

public class UserInfoContextHolder {

    private final static ThreadLocal<User> THREAD_LOCAL = new ThreadLocal<>();

    public static void setLoginInfo(User user) {
        THREAD_LOCAL.set(user);
    }

    public static Long getLoginUserId() {
        User user = THREAD_LOCAL.get();
        if (user == null) {
            throw new BizException("未登录");
        }
        return user.getId();
    }

    public static User getLoginUser() {
        User user = THREAD_LOCAL.get();
        if (user == null) {
            throw new BizException("登录超时或未登录");
        }
        return user;
    }

    public static void remove() {
        THREAD_LOCAL.remove();
    }
}
